from datetime import datetime
data_num = int(input())
for _ in range(data_num):
    parts = input().split()
    time = parts[:2]
    time = " ".join(time)
    between = int(parts[-1])*60
    time = datetime.strptime(time,"%Y-%m-%d %H:%M:%S")
    # print(time)
    stamp = datetime.timestamp(time)
    stamp = (stamp // between) * between
    new = datetime.fromtimestamp(stamp).strftime("%Y-%m-%d %H:%M:%S")
    print(new)